<template>
  <!--template下只能有一个标签，一个div-->
  <div>
    <el-container>
      <el-header>
        <img class="mlogo" src="https://raw.githubusercontent.com/coming1999/images/master/image-20220421143822370.png">
      </el-header>
      <el-main>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>

</template>

<script>
//export default 相当于导出当前vue组件，在其它引入当前组件时可以使用当前组件中的方法和变量
export default {
  name: "Login.vue",
  //这个data（）中return的变量和方法只限于当前声明此data（）的组件使用
  //局部变量的作用
  data() {
    return {
      ruleForm: {
        username: 'krishe',
        password: '111111'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let _this = this
          this.$axios.post("/login",this.ruleForm).then(res =>{
            //从headers中的authorization属性中获取jwt
            //需要在不同路由中使用同一个会改编的参数，也就是需要一个全局参数
            //因为jwt是需要共享的，因此需要把jwt提交到store下的index.js中，设置会全局共享
            let jwt = res.headers['authorization'];
            //拿到后端返回的json数据
            let userInfo = res.data.data;
            //保存token和用户信息
            _this.$store.commit("SET_TOKEN",jwt)
            _this.$store.commit("SET_USERINFO",userInfo)

            _this.$router.push("/blogs")
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.el-header, .el-footer {
  background-color: #FFFF;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  /*background-color: #E9EEF3;*/
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.mlogo {
  height: 150%;
  width: 25%;
  margin: auto;
  margin-top: -8px;
}
.demo-ruleForm {
  max-width: 450px;
  margin: 0 auto;
  margin-top: 10px;

}
</style>